home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / BackCounter.java next >
Encoding:
Java Source  |  2000-08-01  |  5.5 KB  |  245 lines

  1. /*
  2.  * BackCounter.java - A back counter
  3.  * Copyright (C) 2000 Romain Guy
  4.  * guy.romain@bigfoot.com
  5.  * www.jext.org
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22. import waba.fx.*;
  23. import waba.ui.*;
  24.  
  25. /**
  26.  * Displays a back counter.
  27.  * @author Romain Guy <guy.romain@bigfoot.com>
  28.  * @version 1.1
  29.  */
  30.  
  31. public class BackCounter extends Control
  32. {
  33.   ///////////////////////////////////////////////////////////////////////////////////////////////
  34.   // CONSTANTS
  35.   ///////////////////////////////////////////////////////////////////////////////////////////////
  36.   // states
  37.   public static final byte STOPPED = 0, STARTED = 1;
  38.  
  39.   ///////////////////////////////////////////////////////////////////////////////////////////////
  40.   // PRIVATE FIELDS
  41.   ///////////////////////////////////////////////////////////////////////////////////////////////
  42.   // state
  43.   public byte state = STOPPED;
  44.   // the default number (-1) of minutes allowed
  45.   public static final byte TIME = 30;
  46.   // current minutes left
  47.   private byte minutes = TIME;
  48.   // current seconds left
  49.   private byte seconds = 0;
  50.   // parent
  51.   private eCross parent;
  52.   // graphics area
  53.   private Graphics g;
  54.   // the internal timer
  55.   private Timer timer;
  56.   // timer font
  57.   private Font font;
  58.   // timer label width and height
  59.   private int timerWidth, timerHeight;
  60.  
  61.   /**
  62.    * Creates a new timer linked to a specified <code>eCross</code>.
  63.    * This timer counts backward and warn the eCross parent when time
  64.    * is out.
  65.    * @param parent The owner of the timer, which will be warned when time is out
  66.    * @param x The x coordinate of the display
  67.    * @param y The y coordinate of the display
  68.    */
  69.  
  70.   public BackCounter(eCross parent, int x, int y)
  71.   {
  72.     this.parent = parent;
  73.     g = parent.getBackBuffer();
  74.     this.font = new Font("Helvetica", Font.BOLD, 10);
  75.     FontMetrics fm = getFontMetrics(font);
  76.     timerWidth = fm.getTextWidth("00:00") + 8;
  77.     timerHeight = fm.getHeight() + 4;
  78.  
  79.     setRect(x, y, timerWidth, timerHeight);
  80.   }
  81.  
  82.   ///////////////////////////////////////////////////////////////////////////////////////////////
  83.   // PUBLIC METHODS
  84.   ///////////////////////////////////////////////////////////////////////////////////////////////
  85.  
  86.   /**
  87.    * Resolves time event.
  88.    */
  89.  
  90.   public void onEvent(Event evt)
  91.   {
  92.     if (evt.type == ControlEvent.TIMER)
  93.     {
  94.       seconds--;
  95.  
  96.       if (seconds == -1)
  97.       {
  98.         seconds = 59;
  99.         minutes--;
  100.         if (minutes < 0)
  101.         {
  102.           stop();
  103.           paint(true);
  104.           parent.timeOut();
  105.           return;
  106.         }
  107.       }
  108.  
  109.       paint(true);
  110.     }
  111.   }
  112.  
  113.   /**
  114.    * Returns the current amount of minutes left.
  115.    */
  116.  
  117.   public byte getMinutes()
  118.   {
  119.     return minutes;
  120.   }
  121.  
  122.   /**
  123.    * Returns the current amount of seconds left.
  124.    */
  125.  
  126.   public byte getSeconds()
  127.   {
  128.     return seconds;
  129.   }
  130.  
  131.   /**
  132.    * Sets remaining time and starts.
  133.    */
  134.  
  135.   public void setTime(byte minutes, byte seconds)
  136.   {
  137.     this.minutes = minutes;
  138.     this.seconds = seconds;
  139.   }
  140.  
  141.   /**
  142.    * Removes a given amount of minutes to the current time.
  143.    * @param minutes The amount of minutes to be removed
  144.    */
  145.  
  146.   public void remove(byte minutes)
  147.   {
  148.     this.minutes -= minutes;
  149.     if (this.minutes < 0)
  150.     {
  151.       stop();
  152.       paint(true);
  153.       parent.timeOut();
  154.     }
  155.   }
  156.  
  157.   /**
  158.    * Starts the timer.
  159.    * @param let If true, do not reset timer
  160.    */
  161.  
  162.   public void start(boolean let)
  163.   {
  164.     if (!let)
  165.       reset();
  166.  
  167.     timer = addTimer(1000);
  168.     state = STARTED;
  169.   }
  170.  
  171.   /**
  172.    * Resumes the timer.
  173.    */
  174.  
  175.   public void resume()
  176.   {
  177.     if (state == STARTED)
  178.       timer = addTimer(1000);
  179.   }
  180.  
  181.   /**
  182.    * Pauses the timer.
  183.    */
  184.  
  185.   public void pause()
  186.   {
  187.     if (state == STARTED)
  188.       removeTimer(timer);
  189.   }
  190.  
  191.   /**
  192.    * Stops the timer.
  193.    */
  194.  
  195.   public void stop()
  196.   {
  197.     removeTimer(timer);
  198.     minutes = 0;
  199.     seconds = 0;
  200.     state = STOPPED;
  201.   }
  202.  
  203.   /**
  204.    * Resets the time left.
  205.    */
  206.  
  207.   public void reset()
  208.   {
  209.     minutes = TIME;
  210.     seconds = 0;
  211.     state = STOPPED;
  212.   }
  213.  
  214.   /**
  215.    * Paints the timer on screen.
  216.    * @param direct If <code>true</code>, the timer is also painted on screen
  217.    *               without waiting for <code>eCross</code> to repaint it entirely
  218.    */
  219.  
  220.   public void paint(boolean direct)
  221.   {
  222.     g.setColor(255, 255, 255);
  223.     g.fillRect(x + 1, y + 1, timerWidth - 1, timerHeight - 1);
  224.     g.setColor(0, 0, 0);
  225.     g.drawRect(x, y, timerWidth, timerHeight);
  226.  
  227.     g.setFont(font);
  228.     StringBuffer buf = new StringBuffer();
  229.  
  230.     if (minutes < 10)
  231.       buf.append('0');
  232.     buf.append(minutes).append(':');
  233.  
  234.     if (seconds < 10)
  235.       buf.append('0');
  236.  
  237.     g.drawText(buf.append(seconds).toString(), x + 4, y + 2);
  238.  
  239.     if (direct)
  240.       parent.drawTimer(timerWidth, timerHeight);
  241.   }
  242. }
  243.  
  244. // End of BackCounter.java
  245.